<template>
  <div class="index">
    <el-card class="!border-none" shadow="never">
      <el-form
          ref="formRef"
          class="mb-[-16px]"
          :model="queryParams"
          :inline="true"
      >
        <el-form-item label="名称" prop="name">
          <el-input class="w-[280px]" v-model="queryParams.name" />
        </el-form-item>
        <el-form-item label="学号" prop="studentNum">
          <el-input class="w-[280px]" v-model="queryParams.studentNum"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="contact">
          <el-input class="w-[280px]" v-model="queryParams.contact" />
        </el-form-item>
        <el-form-item label="学院" prop="college">
          <el-input class="w-[280px]" v-model="queryParams.college"/>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select
              v-model="queryParams.status"
              style="width: 190px"
              clearable
          >
            <el-option value="0" label="待审核" />
            <el-option value="1" label="通过" />
            <el-option value="2" label="不通过" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
        <el-table-column label="头像" min-width="100">
          <template #default="{ row }">
            <image-contain
                v-if="row.cover"
                :src="row.cover"
                :width="60"
                :height="45"
                :preview-src-list="[row.cover]"
                preview-teleported
                fit="contain"
            />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="name" min-width="100" />
        <el-table-column label="年龄" prop="age" min-width="100" />
        <el-table-column label="性别" prop="sex" min-width="130">
          <template #default="{ row }">
            <el-tag v-if="row.sex==1" type="primary">男</el-tag>
            <el-tag v-if="row.sex==0" type="success">女</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="手机" prop="contact" min-width="100" />
        <el-table-column label="状态" prop="status" min-width="100">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.status == 1">通过</el-tag>
            <el-tag type="warning" v-else-if="row.status == 0">待审核</el-tag>
            <el-tag type="danger" v-else-if="row.status == 2">不通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="学号" prop="studentNum" min-width="100" />
        <el-table-column label="学院" prop="college" min-width="100" />
        <el-table-column label="年级" prop="grade" min-width="100" />
        <el-table-column label="专业" prop="major" min-width="100" />
        <el-table-column label="创建时间" prop="createTime" min-width="100" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button
                v-if="row.status == 0"
                type="primary"
                link
                @click="handleExamine(row)"
            >
              审核
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
  </div>
</template>
<script lang="ts" setup>
import { useAppStore } from '~~/stores/app'
import Pagination from '@/components/pagination/index.vue'

import ImageContain from '@/components/image-contain/index.vue'
import {getMemberLists, postMemberExamine} from "~/api/member";
const appStore = useAppStore()
const queryParams = reactive({
  name: [] as any[],
  contact: [] as any[],
  college: [] as any[],
  status: undefined,
  studentNum: [] as any[],
  orgParty: [] as any[]
})
const { data: pager,refresh } = await useAsyncData(() => getMemberLists({
      page_no: 1,
      page_size: 15,
      ...queryParams
    }),
    {
      default: () => ({
        page: 1,
        size: 15,
        loading: false,
        count: 0,
        lists: [] as any[],
        extend: {} as Record<string, any>
      })
    })


const resetPage = async () => {
  pager.page = 1
  getLists()
}

const getLists = async () => {
  refresh()
}
const resetParams = async () => {
  const paramsInit: Record<any, any> = Object.assign(
      {},
      toRsaw(queryParams)
  )
  Object.keys(paramsInit).forEach((item) => {
    queryParams[item] = paramsInit[item]
  })
  resetPage()
}

const handleExamine = async (obj: any) => {
  let status = obj.status;
  // await feedback.confirm('审核【'+obj.name+'】提交社团社员申请是否通过？').then(() => {
  //   status = 1
  // }).catch(() => {
  //   status = 2
  // })
  try {
    // 显示确认框
    await ElMessageBox.confirm(
        '审核【'+obj.name+'】提交社团社员申请是否通过？',
        '审核提示',
        {
          confirmButtonText: '通过',
          cancelButtonText: '不通过',
          type: 'warning'
        }
    );
    status = 1
    // 点击确定按钮的逻辑
  } catch (error) {
    if (error === 'cancel') {
      // 点击取消按钮的逻辑
      return
    } else if (error === 'close') {
      status = 2
    }
  }
  await postMemberExamine({ id:obj.id,status:status })
  refresh()
}

const getSwiperData = computed(() => {
  try {
    const data = JSON.parse(pageData.value.page.data)
    console.log(data)
    return data.find((item) => item.name === 'pc-banner')?.content
  } catch (error) {
    return {}
  }
})
const showList = computed(() => {
  return getSwiperData.value?.data || []
})
</script>
<style lang="scss" scoped></style>
